'use strict';

const BaseController = require('../../core/base_controller');

class ForumReportController extends BaseController {
  // 获取举报列表
  async index() {
    const { ctx } = this;
    const { status, page = 1, pageSize = 10 } = ctx.query;

    // 构建查询条件
    const where = {
      del_flag: 0
    };
    if (status) where.status = status;

    const { count, rows } = await ctx.model.ForumReport.findAndCountAll({
      where,
      order: [['created_at', 'DESC']],
      offset: (page - 1) * pageSize,
      limit: parseInt(pageSize),
      attributes: { exclude: ['del_flag'] }
    });

    // 获取关联的帖子信息
    const list = await Promise.all(rows.map(async (report) => {
      const forum = await ctx.model.Forum.findByPk(report.forum_id, {
        attributes: ['title', 'category']
      });
      return {
        ...report.toJSON(),
        forum: forum || null
      };
    }));

    this.success({
      list,
      pagination: {
        total: count,
        current: parseInt(page),
        pageSize: parseInt(pageSize),
      },
    });
  }

  // 审核举报
  async review() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { status } = ctx.request.body;

    const report = await ctx.model.ForumReport.findByPk(id);
    if (!report) {
      ctx.throw(404, '举报记录不存在');
    }

    await report.update({ status });
    this.success(null, '审核成功');
  }

  // 删除举报（软删除）
  async destroy() {
    const { ctx } = this;
    const report = await ctx.model.ForumReport.findByPk(ctx.params.id);

    if (!report) {
      ctx.throw(404, '举报记录不存在');
    }

    await report.update({ del_flag: 1 });
    this.success(null, '删除成功');
  }
}

module.exports = ForumReportController; 